package com.ruoyi.api;

import com.ruoyi.api.config.NacosDynamicConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@SpringBootApplication
@EnableCaching
@Slf4j
//@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class ApiTimeApplication {

	public static void main(String[] args) {
		SpringApplication.run(ApiTimeApplication.class, args);
		log.info("date:  {}",new Date());
	}

	/**
	 * https://www.jianshu.com/p/9aa9c05a9757?utm_campaign=haruki
	 * @return
	 */
//	@Bean
//	public HttpMessageConverters fastJsonHttpMessageConverters() {
//		//1、定义一个convert转换消息的对象
//		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//		//2、添加fastjson的配置信息
//		FastJsonConfig fastJsonConfig = new FastJsonConfig();
//		fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible,SerializerFeature.WriteNonStringValueAsString);
//		//3、在convert中添加配置信息
//		fastConverter.setFastJsonConfig(fastJsonConfig);
//		//4、将convert添加到converters中
//		HttpMessageConverter<?> converter = fastConverter;
//		return new HttpMessageConverters(converter);
//	}


	@GetMapping("/hello")
	public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
		return String.format("Hello %s!", name);
	}

//	@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
	private boolean useLocalCache;
	/**
	 *
	 * 1 通过接口发布一个配置
	 * curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=useLocalCache=true"
	 * curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=example&group=DEFAULT_GROUP&content=useLocalCache=false"
	 * 2 应用端读取配置
	 * curl -X GET "http://127.0.0.1:8082/get"
	 */


	@RequestMapping(value = "/get",method = RequestMethod.GET)
	@ResponseBody
	public boolean get() {
		return useLocalCache;
	}

	@Autowired
	private NacosDynamicConf listConf;

	/**
	 * http://127.0.0.1:8082/list
	 * @return
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@ResponseBody
	public void list() {
		System.out.println(listConf.getList());
		System.out.println(listConf.getMaps());
		System.out.println(listConf.getVarmaplist());
	}
}
